﻿/*
 RESPAWN_VEH.SQF © Pumpkin
 =========================

 "résurrection"/"respawn" d'un véhicule avec :
  + possibilité d'accés restreint (réservé aux membres d'une team,...)
  + choix du délai de respawn
  + nombre de respawn limité
  Ce script tourne sur serveur, serveur dont les messages ne s'affichent jamais en Multi-Joueur. Si mon système de dialogue Multiplayer est activé
  (cf. "Init_Speach_Broadcast.sqf" et "Fonctions_Speach.sqf") les "messages" d'interdiction d'accès et du nombre de résurrections restantes s'affichent
  pour tous les joueurs (y compris celui concerné par les messages), sinon le joueur est simplement éjecté dans le cas d'interdiction d'accès et le
  véhicule ne réapparaitra plus sans aucun avertissement dans le cas de respawns limités.

 Paramètres :
 ------------
 nul = [this ('ou nom_du_vehicule') <, délai de réapparition> <, nombre de respawn (infini par défaut)> <, [table de pilotes autorisées alternative] > ] execVM "veh_respawn\Respawn_veh.sqf"

 NOTA : Le délai, la limite du nombre de respawn et la table alternative sont optionnels. Le 3ème et 4ème paramètres sont interchangeables, comme le montre les exemples ci-dessous.


 Exemples :
 ----------

 nul = [this] execVM "scripts\VehicleRespawn.sqf" :
	Simple respawn, respawns nearly immediatly at the initial start position, use "1" in place of "0" to respawn in place
	nul = [this,"MHQ"] execVM "scripts\VehicleRespawn.sqf" : 
	gets special function of MHQ, it will automatically execVM a script called "VehicleMHQ.sqf"

 nul = [this,1,10,["Pumpkin","WarSheeeep"]] execVM "scripts\VehicleRespawn.sqf" :
 	Delayed respawn, respawns after 10 seconds at the current position and reserved to players "Pumpkin" en "WarSheeeep"
	nul = [this,1,10,["Pumpkin","WarSheeeep"], "MED"] execVM "scripts\VehicleRespawn.sqf" : 
	gets special function of MED, it will automatically execVM a script called "VehicleMED.sqf"
 
 nul = [this,0,10,3] execVM "scripts\VehicleRespawn.sqf" :
 	3 allowed respawns, respawn at initial position and after 10 seconds
	nul = [this,0,10,3] execVM "scripts\VehicleRespawn.sqf" : 
	gets special function of MHQ, it will automatically execVM a script called "VehicleMHQ.sqf"

 nul = [this,1,10,3,["Pumpkin","WarSheeeep"]] execVM "scripts\VehicleRespawn.sqf" :
 	3 allowed respawns, respawns after 10 seconds at the current position and reserved to players "Pumpkin" en "WarSheeeep"
	nul = [this,1,10,3,["Pumpkin","WarSheeeep"], "MED"] execVM "scripts\VehicleRespawn.sqf" : 
	gets special function of MED, it will automatically execVM a script called "VehicleMED.sqf"

NOTE: 	For the special function, simply create a script in the "scripts" folder and called "Vehicle____.sqf" where ____ is anything you want, and add it to the initialization.
		The called script will receive the vehicle as a _this variable.
		When the vehicle respawns, the special function is passed to the respawned vehicle.

*/
private ["_veh", "_vehName","_newveh","_startPos", "_startAng","_respawnInPlace","_delay","_nbspawn","_team_members","_vehtype","_ang","_pos", "_specialFunction", "_fileName"];
// Only executing on the server
nul = call compile preprocessFile "scripts\common.sqf";
if (not X_Server) exitwith {};

// Param 1 -- is mandatory and is the vehicle to Respawn.
_veh = _this select 0;
// In case the object was not given, we exit
If (isnil("_veh")) exitwith {};
_vehName = vehicleVarName _veh;
_specialFunction = "";
// We record the initial position and angle of the vehicle if we want to respawn it at start position.
_startPos = getpos _veh;
_startAng = direction _veh;
_vehtype = typeof _veh;

// Param 2 -- respawn in place? (0 or 1)

_respawnInPlace = 0;
if (count _this > 1) then {
	If (typename(_this select 1) == "STRING") then {
		_specialFunction = _this select 1;
	} else {
		_respawnInPlace = _this select 1;
	};
};
if (_respawnInPlace < 0) then { _respawnInPlace = 0 };

// Param 3 : Respawn Delay (default = 60s)
_delay = 60;
if (count _this > 2) then {
	If (typename(_this select 2) == "STRING") then {
		_specialFunction = _this select 2;
	} else {
		_delay = _this select 2;
	};
 };
if (_delay <= 0) then { _delay = 60 };

// Param 3 and 4 : Respawn Delay + Authorized crew, taking into account possible parameters reversal
_nbspawn = -1;
_team_members = [];
if (count _this > 3) then {
	If (typename(_this select 3) == "STRING") then {
		_specialFunction = _this select 3;
	} else {
		if (typename (_this select 3) == "ARRAY") then { _team_members = _this select 3 } else { _nbspawn = _this select 3 };
	};
};
if (count _this > 4) then {
	If (typename(_this select 4) == "STRING") then {
		_specialFunction = _this select 4;
	} else {
		if (typename (_this select 4) == "ARRAY") then { _team_members = _this select 4 } else { _nbspawn = _this select 4 };
	};
};
if (count _this > 5) then {
	If (typename(_this select 5) == "STRING") then {
		_specialFunction = _this select 5;
	};
};

// TODO: If needed, add team member limitation
diag_log Format["Vehicle Respawn Initialized with: %1, %2, %3, %4, %5, %6, %7", _vehtype, _vehName, _startPos, _respawnInPlace, _delay, _nbspawn, _specialFunction];

If (_specialFunction != "") then {
	_fileName = "scripts\VehicleSpecial\Vehicle" + _specialFunction + ".sqf";
	nul = [_veh] execVM _fileName;
};

// Waiting until the vehicle gets destroyed
waituntil{not alive _veh};
diag_log Format["Vehicle destroyed with: %1, %2, %3, %4, %5, %6, %7", _vehtype, _vehName, _startPos, _respawnInPlace, _delay, _nbspawn, _specialFunction];

// Starting to respawn
If (_respawnInPlace == 0) then {
	_ang = _startAng;
	_pos = _startPos;
} else {
	_ang = direction _veh;
	_pos = getpos _veh;
	// When the vehicle was destroyed, if it was a wheeled vehicle, its last position (when destroyed) will be lower than a new vehicle.
	_pos = [_pos select 0, _pos select 1, (_pos select 2) + 0.2];
};
sleep 0.2;

sleep _delay;

// As long as we can respawn, we keep respawning
if ( _nbspawn != 0 ) then {
	deletevehicle _veh;
	sleep 1;
	_newveh = _vehtype createvehicle _pos;
	_newveh setVehicleVarName _vehName;
	_newveh setpos _pos;
	_newveh setdir _ang;
};

// We initialize the vehicule if it can still respawn after that with the respawn script
if (_nbspawn != 0) then {
	if (_nbspawn > 0) then { _nbspawn = _nbspawn - 1 };
	_newveh SetVehicleInit format ["nul = [this,%1,%2,%3,%4,""%5""] execVM ""scripts\VehicleRespawn.sqf""",_respawnInPlace,_delay,_nbspawn,_team_members, _specialFunction];
	ProcessInitCommands;
};
